package com.fms.trollalarm;

import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.WindowManager;

import com.fms.trollalarm.controller.AlarmController;
import com.fms.trollalarm.fragment.AlarmFragment;
import com.fms.trollalarm.fragment.AlarmFragment.OnAlarmListener;
import com.fms.trollalarm.fragment.FindMeFragment;
import com.fms.trollalarm.fragment.MathFragment;
import com.fms.trollalarm.fragment.NotTrollFragment;
import com.fms.trollalarm.fragment.RememberMeFragment;
import com.fms.trollalarm.fragment.TrollButtonFragment;
import com.fms.trollalarm.object.AlarmModel;
import com.fms.trollalarm.object.TrollType;
import com.fms.trollalarm.utils.Common.IntentExtra;
import com.fms.trollalarm.utils.Common.Variable;

public class AlarmActivity extends FragmentActivity {
	private AlarmController alarmController = new AlarmController(this);
	private NotificationManager notificationManager;
	private FragmentManager fragmentManager;
	private AlarmFragment fragment;
	private int levelType;
	private AlarmModel alarm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_frame);

		Random rnd = new Random();
		fragmentManager = getSupportFragmentManager();
		long id;
		id = getIntent().getExtras().getLong(IntentExtra.ID);
		alarm = alarmController.getAlarm(id);
		int trollType = alarm.getTrollType();
		levelType = alarm.getLevelType();
		if (trollType == TrollType.NONE.ordinal()) {
			fragment = new NotTrollFragment();
		} else if (trollType == TrollType.DEFAULT.ordinal()) {
			int i = rnd.nextInt(2) + 2; // only use troll button and find me
			getTrollType(i);
		} else{
			getTrollType(trollType);
		}

		createNotification(alarm);
		alarmController.startAlarm(alarm);
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		fragmentTransaction.replace(R.id.main_frame, fragment).commit();
		fragment.setOnAlarmListener(onAlarmListener);

		getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
						| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
						| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	private OnAlarmListener onAlarmListener = new OnAlarmListener() {

		@Override
		public void onStopAlarm() {
			alarmController.stopAlarm(alarm);
			if (notificationManager != null) {
				notificationManager.cancel(1);

			}
			finish();
		}

		@Override
		public void onSnoozeAlarm() {
			alarmController.snooze(alarm, Variable.TIME_SNOOZE);
		}

		@Override
		public void onChangeTrollType(TrollType trollType) {
			int type = trollType.ordinal();
			type++;
			if (type >= TrollType.values().length) {
				type = 2;
			}
			Log.d("fms", "type " + type);
			getTrollType(type);
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			fragmentTransaction.replace(R.id.main_frame, fragment).commit();
			fragment.setOnAlarmListener(onAlarmListener);
		}
	};

	private void getTrollType(int i) {
		if (i == TrollType.MATH.ordinal()) {
			fragment = MathFragment.newInstance(levelType);
		} else if (i == TrollType.TROLL_BUTTON.ordinal()) {
			fragment = TrollButtonFragment.newInstance(levelType);
		} else if (i == TrollType.REMEMBER_ME.ordinal()) {
			fragment = RememberMeFragment.newInstance(levelType);
		} else {
			fragment = FindMeFragment.newInstance(levelType);
		}
	}

	private void createNotification(AlarmModel alarm) {
		notificationManager = ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE));
		int icon = R.drawable.ic_launcher;
		String message = getString(R.string.title_notification);
		Notification notification = new Notification(icon, message,
				System.currentTimeMillis());
		Intent notificationIntent = new Intent(this, AlarmActivity.class);
		// set intent so it does not start a new activity
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);
		// notificationIntent.putExtra(IntentExtra.ID, alarm.getId());
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);
		// Intent intent = new Intent(this, OnAlarmReceiver.class);
		// intent.putExtra(IntentExtra.ID, alarm.getId());
		// PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
		// (int) alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
		String title = getString(R.string.app_name);
		notification.setLatestEventInfo(this, title, message, pendingIntent);
		notification.flags |= Notification.FLAG_NO_CLEAR;
		notificationManager.notify(1, notification);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alarm, menu);
		return false;
	}

	@Override
	public void onBackPressed() {
		// super.onBackPressed();
	}

	@Override
	protected void onDestroy() {
		if (notificationManager != null) {
			notificationManager.cancel(1);
		}
		super.onDestroy();
	}

}
